package com.coder.oj.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.admin.mapstruct.DiscussMapstruct;
import com.coder.oj.admin.query.DiscussQuery;
import com.coder.oj.admin.vo.DiscussVo;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.entity.Discuss;
import com.coder.oj.common.enums.CommonStatusEnum;
import com.coder.oj.common.exception.DataException;
import com.coder.oj.common.mapper.DiscussMapper;
import com.coder.oj.common.mapper.UserMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bosen 2022/11/28 11:50
 */
@Service
public class DiscussService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussMapper discussMapper;

    @Autowired
    private DiscussMapstruct discussMapstruct;

    /**
     * 分页查询
     * @author Bosen 2022/12/16 10:03
     **/
    public JsonPage<DiscussVo> getDiscussPage(DiscussQuery params) {
        LambdaQueryWrapper<Discuss> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(params.getContent()), Discuss::getContent, params.getContent());
        queryWrapper.eq(ObjectUtils.isNotEmpty(params.getStatus()), Discuss::getStatus, params.getStatus());
        queryWrapper.eq(ObjectUtils.isNotEmpty(params.getAnswerId()), Discuss::getAnswerId, params.getContent());
        IPage<Discuss> page = discussMapper.selectPage(new Page<>(params.getPage(), params.getPageSize()), queryWrapper);
        List<DiscussVo> collect = page.getRecords().stream().map(discuss -> {
            DiscussVo vo = discussMapstruct.toVO(discuss);
            vo.setCreateBy(userMapper.getUserNickname(discuss.getCreateBy()));
            vo.setUpdateBy(userMapper.getUserNickname(discuss.getUpdateBy()));
            return vo;
        }).collect(Collectors.toList());
        return new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
    }

}
